import { Controller } from "../../mvc/core/Controller";
import { IHitEggController } from "./interfaces/IHitEggController";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { ELottertType } from "../../../Message/Public/ELottertType";
import { SPublicNotice } from "../../../Message/Public/SPublicNotice";
import { EShowArea } from "../../../Message/Public/EShowArea";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { Alert } from "../../../com/mui/controls/Alert";
import { Language } from "../../../extend/language/Language";
import { AIManager } from "../scene/ai/AIManager";
import { AIType } from "../scene/ai/AIType";
import { HitEggActiveIcon } from "../view/hitEgg/HitEggActiveIcon";
import { EventName } from "../mvc/EventName";
import { MsgManager } from "../manager/MsgManager";
import { IHitEggModule } from "../../../modules/interfaces/IHitEggModule";
import { IView } from "../../mvc/interfaces/IView";
import { HitEggModule } from "../../../modules/HitEggModule";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { ItemData } from "../resource/info/ItemData";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ClockManager } from "../manager/ClockManager";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SHitEggActiveInfo } from "../../../Message/Game/SHitEggActiveInfo";
import { SShakeTreeNum } from "../../../Message/Public/SShakeTreeNum";
import { SHitEggActiveProcessInfo } from "../../../Message/Game/SHitEggActiveProcessInfo";
import { EHitEggReReward } from "../../../Message/Game/EHitEggReReward";
import { PrizeFallHintTimeBtn } from "../view/hitEgg/PrizeFallHintTimeBtn";
import { MultiExperienceHintBtn } from "../view/hitEgg/MultiExperienceHintBtn";
import { SMoney } from "../../../Message/Game/SMoney";
import { TaskTargetInfo } from "../model/TaskTargetInfo";
type int = number;
//class HitEggController
    
    export  class HitEggController extends Controller implements IHitEggController
    {
       

        /* internal  */onHitEggHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["uid"] as string;
            var loc2=arg1.data["amount"] as int;
            var loc3=arg1.data["useItem"] as Boolean;
            GameProxy.active.hitEgg(loc1, loc2, loc3);
            return;
        }

        /* internal  */onHitEggSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._hitEggModule) 
            {
                this._hitEggModule.updateProcess(loc1);
            }
            return;
        }

        /* internal  */onGetRecordsSuccessHandler(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=arg1.data["type"] as int;
            if (loc1 == ELottertType._ELotteryHitEgg || loc1 == ELottertType._ELotteryHitEggHigh) 
            {
                loc2 = arg1.data["records"] as Array<any>;
                cache.hitEgg.noticeMap[loc1] = loc2;
                if (!(this._hitEggModule == null) && !this._hitEggModule.isHide) 
                {
                    this._hitEggModule.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onExplorerRumorAddHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as SPublicNotice;
            if (loc1.area & EShowArea._EShowAreaExplorer && (loc1.code == ELottertType._ELotteryHitEgg || loc1.code == ELottertType._ELotteryHitEggHigh)) 
            {
                cache.hitEgg.setNoticeMap([loc1], loc1.code);
                if (!(this._hitEggModule == null) && !this._hitEggModule.isHide) 
                {
                    this._hitEggModule.updateExplorerRecords();
                }
            }
            return;
        }

        /* internal  */onHitEggUseItemHandler(arg1: DataEvent): void
        {
            this._targetInfo = cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectHitEgg);
            Alert.okLabel = Language.getString(36400);
            Alert.calcelLabel = Language.getString(36401);
            Alert.showCloseBtn = true;
            Alert.show(Language.getString(36614), null, Alert.OK | Alert.CANCEL, null, this.onAlertCloseHandler);
            return;
        }

        /* internal  */onAlertCloseHandler(arg1: int): void
        {
            if (arg1 != Alert.OK) 
            {
                if (arg1 == Alert.CANCEL) 
                {
                    AIManager.onAIControl(this._targetInfo, AIType.AI_Convey);
                }
            }
            else 
            {
                AIManager.onAutoPathAIControl(this._targetInfo);
            }
            return;
        }

        public get hitEggIcon(): HitEggActiveIcon
        {
            if (this._hitEggIcon == null) 
            {
                this._hitEggIcon = new HitEggActiveIcon();
                this._hitEggIcon.updateIconInfo();
                this._hitEggIcon.addEventListener(EventName.HitEggActiveIconClick, this.onHitEggActiveIconClickHandler);
            }
            return this._hitEggIcon;
        }

        /* internal  */onHitEggActiveIconClickHandler(arg1: DataEvent): void
        {
            this._uid = "";
            this.openHitEggWindow();
            return;
        }

        /* internal  */openHitEggWindow(): void
        {
            if (!this._isActive) 
            {
                MsgManager.showRollTipsMsg(Language.getString(36613));
                return;
            }
            view.show();
            if (this._sHitEggActiveInfo) 
            {
                (view as IHitEggModule).updateShopType(this._sHitEggActiveInfo.shopType);
                (view as IHitEggModule).updateHitEggActiveInfo(this._sHitEggActiveInfo.rewardInfo);
                if (cache.hitEgg.noticeMap[this._sHitEggActiveInfo.shopType] == null) 
                {
                    GameProxy.sceneProxy.getExplorerRecords(this._sHitEggActiveInfo.shopType);
                }
                (view as IHitEggModule).updateResTime();
            }
            (view as IHitEggModule).propUid = this._uid;
            if (this.cache.hitEgg.hitNumInfo) 
            {
                (view as IHitEggModule).updateNumTxt(this.cache.hitEgg.hitNumInfo.num, this.cache.hitEgg.hitNumInfo.perNum);
            }
            return;
        }

        protected /* override */ initView(): IView
        {
            if (this._hitEggModule == null) 
            {
                this._hitEggModule = new HitEggModule();
            }
            return this._hitEggModule;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(ServerCommand.ActiveTypeHitEgg, this.onActiveTypeHitEggHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicHitEggActiveInfo, this.onHitEggActiveInfoHandler);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicHitEggActiveProcessInfo, this.onHitEggActiveProcessInfoHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onBackPackItemsChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.HitEggNumUpdate, this.onHitEggNumUpdate);
            NetDispatcher.addCmdListener(ServerCommand.LotteryActiveRewardFlyToBag, this.onFlyToBagInfo);
            Dispatcher.addEventListener(EventName.HitEggOpenWindow, this.onHitEggOpenWindowHandler);
            Dispatcher.addEventListener(EventName.HitEggOpenReq, this.onHitEggOpenReqHandler);
            Dispatcher.addEventListener(EventName.HitEggOpenReqSuccess, this.onHitEggOpenReqSuccessHandler);
            Dispatcher.addEventListener(EventName.HitEgg, this.onHitEggHandler);
            Dispatcher.addEventListener(EventName.HitEggSuccess, this.onHitEggSuccessHandler);
            Dispatcher.addEventListener(EventName.TreasureGetRecordsSuccess, this.onGetRecordsSuccessHandler);
            Dispatcher.addEventListener(EventName.TreasureExplorerRumorAdd, this.onExplorerRumorAddHandler);
            Dispatcher.addEventListener(EventName.HitEggUseItem, this.onHitEggUseItemHandler);
            return;
        }

        /* internal  */onHitEggOpenWindowHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as ItemData;
            if (loc1) 
            {
                this._uid = loc1.uid;
            }
            this.openHitEggWindow();
            return;
        }

        /* internal  */onActiveTypeHitEggHandler(arg1: SActiveToClient): void
        {
            this._activeToClient = arg1;
            cache.hitEgg.activeToClient = arg1;
            this._isActive = cache.active.isActiveInEffect(arg1.code);
            if (this._isActive && this.hitEggIcon.isHide) 
            {
                this.hitEggIcon.show();
                ClockManager.instance.addDateCall(arg1.endDt, this.removeHitEggActive);
            }
            return;
        }

        /* internal  */removeHitEggActive(): void
        {
            if (this._hitEggIcon && !this._hitEggIcon.isHide) 
            {
                this._hitEggIcon.hide();
            }
            if (this._hitEggModule && !this._hitEggModule.isHide) 
            {
                this._hitEggModule.hide();
            }
            if (this._activeToClient) 
            {
                ClockManager.instance.removeDateCall(this._activeToClient.endDt);
            }
            return;
        }

        /* internal  */onHitEggActiveInfoHandler(arg1: MessageBlock): void
        {
            this._sHitEggActiveInfo = arg1.messageBase as SHitEggActiveInfo;
            cache.hitEgg.sHitEggActiveInfo = this._sHitEggActiveInfo;
            if (this._sHitEggActiveInfo && this._hitEggModule && !this._hitEggModule.isHide) 
            {
                (view as IHitEggModule).updateShopType(this._sHitEggActiveInfo.shopType);
                (view as IHitEggModule).updateHitEggActiveInfo(this._sHitEggActiveInfo.rewardInfo);
            }
            return;
        }

        /* internal  */onFlyToBagInfo(arg1: ItemData): void
        {
            if (this._hitEggModule && !this._hitEggModule.isHide) 
            {
                (this._hitEggModule as HitEggModule).flyToBag(arg1);
            }
            return;
        }

        /* internal  */onHitEggNumUpdate(arg1: SShakeTreeNum): void
        {
            if (this._hitEggModule) 
            {
                this._hitEggModule.updateNumTxt(arg1.num, arg1.perNum);
            }
            return;
        }

        /* internal  */onHitEggActiveProcessInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SHitEggActiveProcessInfo;
            cache.hitEgg.touchPlayers = loc1.touchPlayers;
            var loc2=cache.hitEgg.getSHitEggRewardByNode(loc1.touchNode);
            var loc3=0;
            if (loc2) 
            {
                var loc4=loc2.serverType;
                switch (loc4) 
                {
                    case EHitEggReReward._EHitEggRewardDropMoney:
                    {
                        if ((loc3 = (loc1.endDt.time - ClockManager.instance.nowDate.time) / 1000) > 0) 
                        {
                            this.prizeFallHintTimeBtn.show();
                            this.prizeFallHintTimeBtn.highLight();
                            this.prizeFallHintTimeBtn.setTime(loc3);
                        }
                        break;
                    }
                }
            }
            loc3 = (loc1.endExpDt.time - ClockManager.instance.nowDate.time) / 1000;
            if (!(loc1.expAddVal == 0) && loc3 > 0) 
            {
                this.multiExperienceBtn.show();
                this.multiExperienceBtn.setTime(loc3);
                this.multiExperienceBtn.updateMultiNum(loc1.expAddVal / 100 + 1);
            }
            if (this._hitEggModule && !this._hitEggModule.isHide) 
            {
                (view as IHitEggModule).updateHitEggActiveInfo(this._sHitEggActiveInfo.rewardInfo);
            }
            return;
        }

        /* internal  */get prizeFallHintTimeBtn(): PrizeFallHintTimeBtn
        {
            if (this._prizeFallHintTimeBtn == null) 
            {
                this._prizeFallHintTimeBtn = new PrizeFallHintTimeBtn();
            }
            return this._prizeFallHintTimeBtn;
        }

        /* internal  */get multiExperienceBtn(): MultiExperienceHintBtn
        {
            if (this._multiExperienceBtn == null) 
            {
                this._multiExperienceBtn = new MultiExperienceHintBtn();
            }
            return this._multiExperienceBtn;
        }

        /* internal  */onMoneyUpdateHandler(arg1: Object): void
        {
            var loc1=arg1 as SMoney;
            if (this._hitEggModule && !this._hitEggModule.isHide) 
            {
                this._hitEggModule.updateGold();
            }
            return;
        }

        /* internal  */onBackPackItemsChangeHandler(arg1: Object): void
        {
            if (this._hitEggModule && !this._hitEggModule.isHide) 
            {
                this._hitEggModule.updatePropNum();
            }
            return;
        }

        /* internal  */onHitEggOpenReqHandler(arg1: DataEvent): void
        {
            GameProxy.active.openHitEgg();
            return;
        }

        /* internal  */onHitEggOpenReqSuccessHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (this._hitEggModule) 
            {
                this._hitEggModule.updateProcess(loc1);
            }
            return;
        }

        private /* var */_hitEggIcon: HitEggActiveIcon;

        private /* var */_hitEggModule: IHitEggModule;

        private /* var */_activeToClient: SActiveToClient;

        private /* var */_uid: string="";

        private /* var */_sHitEggActiveInfo: SHitEggActiveInfo;

        private /* var */_isActive: Boolean;

        private /* var */_targetInfo: TaskTargetInfo;

        private /* var */_multiExperienceBtn: MultiExperienceHintBtn;

        private /* var */_prizeFallHintTimeBtn: PrizeFallHintTimeBtn;
    }
